package HTML::FormParser;

use warnings;
use strict;

use base qw/HTML::Parser/;

=head1 NAME

HTML::FormParser

=head1 SYNOPSIS

    use HTML::FormParser;
    use Data::Dumper;
    
    my $hf = HTML::FormParser->new;
    $hf->parse( $some_html_body_text );
    $hf->eof;
    print Dumper $hf->result;

=head1 DESCRIPTION

The result of a form item is pulled out from HTML. 

=head1 METHODS

=head2 result

boolean getter & setter

=cut

sub result {
    my $self = shift;
    return $self->{'result_pack'};
}

=head2 isSelect

boolean getter & setter
 
=cut

sub isSelect {
    my $self     = shift;
    my $isSelect = shift;
    if ( defined($isSelect) ) {
        $self->{'isSelect'} = $isSelect;
    }
    else {
        return $self->{'isSelect'};
    }
}

=head2 isTextArea

boolean getter & setter

=cut

sub isTextArea {
    my $self       = shift;
    my $isTextArea = shift;
    if ( defined($isTextArea) ) {
        $self->{'isTextArea'} = $isTextArea;
    }
    else {
        return $self->{'isTextArea'};
    }
}

=head2 tmp

temporary value getter & setter

=cut

sub tmp {
    my $self = shift;
    my $tmp  = shift;
    if ( defined($tmp) ) {
        $self->{'tmp'} = $tmp;
    }
    else {
        return $self->{'tmp'};
    }
}

=head2 start

HTML::Parser Override (start)

See: L<HTML::Parser>

Tag, input select option textarea are pulled out from HTML. 

=back

=cut

sub start {
    my ( $self, $tagname, $attr, $attrseq, $text ) = @_;
    my ( $k, $i );

    my $name     = $$attr{name}     ? $$attr{name}  : '';
    my $type     = $$attr{type}     ? $$attr{type}  : '';
    my $value    = $$attr{value}    ? $$attr{value} : '';
    my $selected = $$attr{selected} ? '1'           : '';
    my $checked  = $$attr{checked}  ? '1'           : '';

    if ( $tagname =~ /select/i ) {
        return unless ($name);
        $self->isSelect($name);
        $self->{'result_pack'}{$name}{'type'}               = $tagname;
        $self->{'result_pack'}{ $self->isSelect }{'list'}   = {};
        $self->{'result_pack'}{ $self->isSelect }{'values'} = {};
    }
    elsif ( $tagname =~ /input/i ) {
        return unless ($name);
        if ( $type =~ /(text|hidden)/ ) {
            $self->{'result_pack'}{$name}{'type'}  = $type;
            $self->{'result_pack'}{$name}{'value'} = ($value);
        }
        elsif ( $type =~ /(radio|checkbox)/ ) {
            $self->{'result_pack'}{$name}{'type'} = $type;
            if ($value) {
                $self->{'result_pack'}{$name}{'list'}{$value} = $value;
                $self->{'result_pack'}{$name}{'values'}{$value} = $value if ($checked);
            }
        }
    }
    elsif ( $tagname =~ /option/i ) {
        if ( $self->isSelect ) {
            $self->tmp($value);
            $self->{'result_pack'}{ $self->isSelect }{'values'}{$value} = $value if ($selected);
        }
    }
    elsif ( $tagname =~ /textarea/i ) {
        return unless ($name);
        $self->isTextArea($name);
        $self->{'result_pack'}{$name}{'type'}  = $tagname;
        $self->{'result_pack'}{$name}{'value'} = '';
    }
}

=head2 end

HTML::Parser Override (end)

See: L<HTML::Parser>

=cut

sub end {
    my ( $self, $tagname, $text ) = @_;
    if ( $tagname =~ /textarea/i ) {
        $self->isTextArea(0);
    }
}

=head2 text

HTML::Parser Override (text)

See: L<HTML::Parser>

=cut

sub text {
    my ( $self, $text, $is_cdata ) = @_;
    if ( $self->tmp ) {
        $self->{'result_pack'}{ $self->isSelect }{'list'}{ $self->tmp } = $text;
        $self->tmp('');
    }
    elsif ( $self->isTextArea ) {
        $self->{'result_pack'}{ $self->isTextArea }{'value'} .= $text;
    }
}

=head1 SEE ALSO

L<HTML::Parser>

=head1 AUTHOR

Shanon Inc, E<lt>http://www.shanon.co.jp/E<gt>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;

